package org.viva.action.secu;

import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.viva.core.api.Ajax;
import org.viva.entity.secu.Org;
import org.viva.secu.Security;
import org.viva.service.secu.CompanyService;
import org.viva.service.secu.OrgService;

@Controller
public class OrgAction {

    @Resource
    private OrgService     orgService;
    @Resource
    private CompanyService companyService;

    @RequestMapping("/secu/org")
    @RequiresAuthentication
    public String index(HttpServletRequest request, ModelMap modelMap) {
        modelMap.put("user", Security.getUser());
        modelMap.put("company", companyService.get(Security.getUser().getCompanyId()));
        return "/secu/org";
    }

    @RequestMapping("/secu/org/allJson")
    @ResponseBody
    public Map<String, Object> allJson(HttpServletRequest request) {
        long id = companyService.get(Security.getUser().getCompanyId()).getOrgId();
        return Ajax.exec(orgService.getById(id));
    }

    @RequestMapping("/secu/org/infoJson")
    @ResponseBody
    public Org infoJson(long id) {
        return orgService.get(id);
    }

    @RequestMapping("/secu/org/save")
    @ResponseBody
    public Map<String, Object> doSave(Org org) {
        orgService.save(org);
        return Ajax.exec();
    }

}
